package com.study.ws.util;

import org.apache.catalina.connector.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.server.ServerHttpResponse;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * @author xl-9527
 * @since 2025/3/25
 **/
public class ResponseUtil {

    private static final Logger log = LoggerFactory.getLogger(ResponseUtil.class);

    public static void responseError(final String msg, final Response response) {
        response.setStatus(500);
        try {
            response.getWriter().println(Arrays.toString(msg.getBytes(StandardCharsets.UTF_8)));
        } catch (IOException e) {
            log.error("response error", e);
        }
    }

    public static void responseError(final String msg, final ServerHttpResponse response) {
        response.setStatusCode(HttpStatusCode.valueOf(500));
        try {
            response.getBody().write(msg.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            log.error("response error", e);
        }
    }
}
